<template>
  <a-popover placement="bottomLeft" :trigger="triggerMode" :auto-adjust-overflow="false" @open-change="(open) => open && getData()">
    <template #content>
      <BaseTable
        :column-code="config['columnCode']"
        :columns="config['columns']"
        :loading="loading"
        :row-key="config['rowKey']"
        :style="{ width: tableWidth }"
        :data-source="tableData"
        :scroll="tableScroll"
      >
        <template v-for="item in Object.keys($slots)" #[item]="data" :key="item">
          <slot :name="item" v-bind="data || {}"></slot>
        </template>
      </BaseTable>
    </template>
    <span ref="textRef" class="cursor-pointer text-primary hover:underline"><slot></slot></span>
  </a-popover>
</template>

<script setup>
import { nextTick, ref, onMounted } from 'vue'
import { getSystemConfig } from '@/utils/systemConfig'

const props = defineProps({
  params: {
    type: Object,
    default: () => ({})
  },
  scroll: {
    type: Object,
    default: () => ({ y: 450 })
  },
  width: {
    type: Number,
    default: 0
  },
  config: {
    type: Object,
    default: () => ({})
  }
})
const tableScroll = ref(null)
const textRef = ref(null)
const tableWidth = ref(0)
nextTick(() => setHeight())
const setHeight = () => {
  if (props.width) {
    tableWidth.value = `${props.width}px`
  } else {
    tableWidth.value = `calc(100vw - ${textRef.value.getBoundingClientRect().left + 8}px)`
    tableScroll.value = props.scroll
  }
}
const { loading, tableData, getData } = useTable({
  url: props.config['url'],
  immediate: false,
  pagination: false,
  formatParams: (params) => {
    return {
      ...params,
      ...props.params
    }
  },
  formatResult: (res) => {
    if (props.config.formatResult) {
      return props.config.formatResult?.(res)
    } else {
      return res
    }
  }
})
const triggerMode = ref('hover')
onMounted(() => {
  //DRUG_STORE_SHOW_DETAIL_MODE 药库显示明细模式  0:HOVER|1:CLICK
  triggerMode.value = getSystemConfig('DRUG_STORE_SHOW_DETAIL_MODE') === '1' ? 'click' : 'hover'
})
</script>
<style lang="less" scoped></style>
